import { Component, EventEmitter, forwardRef, Input, Output, SimpleChanges } from "@angular/core";
import { ControlValueAccessor, FormBuilder, FormGroup, NG_VALUE_ACCESSOR } from "@angular/forms";

import { parse } from 'date-fns';
@Component({
  selector: 'app-design-form-date-picker',
  templateUrl: './design-form-date-picker.component.html',
  styleUrls: ['./design-form-date-picker.component.scss'],
  providers: [
    {
      provide: NG_VALUE_ACCESSOR,
      useExisting: forwardRef(() => DesignFormDatePickerComponent),
      multi: true
    }
  ]
})
export class DesignFormDatePickerComponent implements ControlValueAccessor {
  @Input() record: any //="record"
  @Input() formConfig //="record"
  @Input() form: FormGroup  //??
  format: string = 'yyyy-MM-dd'
  // isDateTime:boolean=false

  get mode() {
    return this.record.options.format === 'YYYY' || this.record.options.format === 'yyyy' ? 'year'
      : this.record.options.format === 'YYYY-MM' || this.record.options.format === 'yyyy-MM' ? 'month'
        : 'date'
  }

  get isDateTime() {
    return this.record.options && this.record.options.format && this.record.options.format.indexOf('HH:mm:ss') > -1
  }

  isDisabled: boolean = false
  _value: any
  onChange: (_: any) => void = (_: any) => { };
  onTouched: () => void = () => { };
  checkDateTimeFormat(input) {
    const regex = /^(\d{4})年(\d{2})月(\d{2})日(?:\d{2}时\d{2}分\d{2}秒)?$/;
    return regex.test(input);
  }
  dateRegex = /^(Mon|Tue|Wed|Thu|Fri|Sat|Sun) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec) \d{1,2} \d{2}:\d{2}:\d{2} (CST|EST|MST|PST) \d{4}$/;
  dateRegex1 = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}([+-]\d{2}:\d{2}|Z)?$/;
  convertToDateTime(input) {
    const regex = /^(\d{4})年(\d{2})月(\d{2})日(?:\d{2}时\d{2}分\d{2}秒)?$/;
    const match = input.match(regex);
    if (match) {
      const year = parseInt(match[1]);
      const month = parseInt(match[2]) - 1 || 0; // 月份从0开始
      const day = parseInt(match[3]) || 1;
      const hour = parseInt(match[4]) || 0;
      const minute = parseInt(match[5]) || 0;
      const second = parseInt(match[6]) || 0;

      return new Date(year, month, day, hour, minute, second);
    } else {
      return null; // 返回null表示转换失败
    }
  }
  writeValue(obj: any): void {
    console.log('objobjobj',obj)
    this.format = this.record.options && this.record.options.format && this.record.options.format.replace('DD', 'dd').replace('YYYY', 'yyyy');
    // console.log('typeofobj',typeof obj, new Date(Date.parse(obj)));

    // if (typeof obj === 'string') {
    //   obj = new Date(Date.parse(obj))
    // }
    let dateR = null;
    if (this.checkDateTimeFormat(obj)) {
      dateR = this.convertToDateTime(obj);
    } else if (this.dateRegex.test(obj)||this.dateRegex1.test(obj)) {
      dateR = new Date(Date.parse(obj))
    } else {
      try {
        dateR = obj ? parse(obj, this.format, new Date()) : obj;
      } catch (err) {
        console.error('err', err);
      }
    }
    this._value = dateR
  }
  registerOnChange(fn: any): void {
    this.onChange = fn
  }
  registerOnTouched(fn: any): void {
    this.onTouched = fn;
  }
  setDisabledState?(isDisabled: boolean): void {
    this.isDisabled = isDisabled
  }

  change($event) {
    this.onTouched()
    this.onChange($event)
  }
}